// packageA/pages/companyInfo/companyInfo.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    from:{
      id: '',
      name:'',
      synopsis:'',
      summary:'',
      phone:'',
      email:'',
      address:'',
      img: ''
    },
    loading: false,
    fileList: [],
    isAdd: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  nameChange(e){
    this.setData({['from.name']: e.detail})
  },
  synopsisChange(e){
    this.setData({['from.synopsis']: e.detail})
  },
  summaryChange(e){
    this.setData({['from.summary']: e.detail})
  },
  phoneChange(e){
    this.setData({['from.phone']: e.detail})
  },
  emailChange(e){
    this.setData({['from.email']: e.detail})
  },
  addressChange(e){
    this.setData({['from.address']: e.detail})
  },
  getList(){
    db.collection('companyInfo').get().then(res=>{
      if(res.data.length == 0){
        this.setData({isAdd: true})
      }else {
        let data = res.data[0]
        let dataInfo = {
          id: data._id,
          name: data.name,
          synopsis: data.synopsis,
          summary: data.summary,
          phone: data.phone,
          email: data.email,
          address: data.address,
          img: data.img
        }
        this.setData({
          from: dataInfo,
          fileList: [{url: data.img}]
        })
        console.log(this.data.from);
      }
    })
  },
  sumbit(){
    if (this.data.fileList.length == 0) {
      wx.showToast({ title: '请选择图片', icon: 'none' });
      return
    }
    if(!this.data.from.name){
      wx.showToast({ title: '请输入名称', icon: 'none' });
      return
    }
    if(!this.data.from.synopsis){
      wx.showToast({ title: '请输入简介', icon: 'none' });
      return
    }
    if(!this.data.from.summary){
      wx.showToast({ title: '请输入总结', icon: 'none' });
      return
    }
    if(!this.data.from.phone){
      wx.showToast({ title: '请输入电话', icon: 'none' });
      return
    }
    if(!this.data.from.email){
      wx.showToast({ title: '请输入邮箱', icon: 'none' });
      return
    }
    if(!this.data.from.address){
      wx.showToast({ title: '请输入地址', icon: 'none' });
      return
    }
    this.uploadToCloud()
  },
  deleteImg(e){
    this.setData({ fileList: [] });
  },
  afterRead(event){
    let oldData = this.data.fileList
    let newData = oldData.concat(event.detail.file)
    this.setData({ fileList: newData });
  },
  uploadToCloud() {
    this.setData({
      loading: true
    })
    let that = this
    wx.cloud.callFunction({
      name: 'getOpenid',
      success(res){
        console.log(that.data.fileList);
        console.log(that.data.from);
        let openid = res.result.openid
        let fileList = that.data.fileList
        let date = Date.now()
        if(that.data.from.img != fileList[0].url){
          let name = openid + '.' + date + '.png'
          wx.cloud.uploadFile({
            cloudPath: name,
            filePath: fileList[0].url
          })
          .then(res => {
            let data = that.data.from
            data.img = res.fileID
            if(that.data.isAdd){
              that.add(data)
            }else {
              that.edit()
            }
          }).catch(e => {
            console.log(e);
            wx.showToast({ title: '上传失败', icon: 'none' });
            that.setData({
              loading: false
            })
          });
        }else {
          that.edit()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getList()
  },
  add(data){
    let that = this
    db.collection('companyInfo').add({data: data}).then(d=>{
      wx.showToast({ title: '成功', icon: 'none' });
      setTimeout(()=>{
        wx.reLaunch({
          url: '/pages/index/index',
          success:(res)=>{
            that.setData({
              loading: false
            })
          }
        })
      },500)
    })
  },
  edit(){
    let that = this
    db.collection('companyInfo').doc(that.data.from.id).update({data: that.data.from}).then(d=>{
      wx.showToast({ title: '成功', icon: 'none' });
      setTimeout(()=>{
        wx.reLaunch({
          url: '/pages/index/index',
          success:(res)=>{
            that.setData({
              loading: false
            })
          }
        })
      },500)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})